﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LSProtocol;
using Usermanagment.DBConnection;
using pokerServer.ProtHandlers;
using ConnectionControl;
namespace ProtHandlers
{
	
	class authProtHandle : absRegAutHandle
	{
		private ClientConnection cc;

		public authProtHandle(String[] synt, ClientConnection cc)
		{
			mstrings = synt;
			base.stringSort(this.mstrings);
			this.dbc = cc.getDBC();
			this.cc = cc;
		}


		
		public override String work()
		{
			dbc.OpenConnection();
			sip = new SignInProtocol(dbc);
			er = sip.signIn(mstrings[0], mstrings[1]);
			DBDatafetch dft = new DBDatafetch(dbc);
			String ans = "";
			switch (er)
			{
				case Errors.ERR_BadPassword:
					ans = "AUTH-FAIL BadPassword " + sip.error;
					break;
				case Errors.ERR_UsernameNotExists:
					ans = "AUTH-FAIL UserNameNotExist";
					break;
				case Errors.ERR_PassIlChars:
					ans = "AUTH-FAIL PassIleagalChars!";
					break;
				case Errors.ERR_UsernameIlChars:
					ans = "AUTH-FAIL UserHasIleagalChars!";
					break;
				case Errors.ERR_UsernameLoggedIn:
					uint chips = Convert.ToUInt32(dft.getChips(mstrings[0]));
					this.cc.conChips = (int)chips;
					ans = "AUTH-ACK " + mstrings[0] + " " + chips;
					
					cc.myPlayer = new pokerServer.GameManagment.GameTools.Player(cc, 1, mstrings[0], chips);
					break;
				case Errors.ERR_UserBanned:
					ans = "AUTH-FAIL UserIsBannedContactAdmin";
					break;
				
			}
			dbc.closeConnection();
			return ans;
		}


	}
}
